package com.samfdl.serversocket;

import android.content.Context;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

// 客户端的管理类
public class ClientManager {
    private static ServerThread serverThread = null;
    private static Map<String, Socket> clientList = new HashMap<>();

    public static void startServer(Context context) {
        System.out.println("开启服务");
        if (serverThread != null) {
            showDown();
        }
        serverThread = new ServerThread();
        new Thread(serverThread).start();
        System.out.println("开启服务成功");
    }

    // 关闭所有server socket 和 清空Map
    public static void showDown() {
        if (serverThread != null && clientList != null) {
            for (Socket socket : clientList.values()) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            serverThread.Stop();
            clientList.clear();
        }
    }

    private static class ServerThread implements Runnable {
        private int port = 45450;
        private boolean isExit = false;
        private ServerSocket server;

        public ServerThread() {
            try {
                server = new ServerSocket(port);
                System.out.println("启动服务成功" + "port:" + port);
            } catch (IOException e) {
                System.out.println("启动server失败，错误原因：" + e.getMessage());
            }
        }

        @Override
        public void run() {
            try {
                while (!isExit) {
                    // 进入等待环节
                    System.out.println("等待设备的连接... ... ");
                    final Socket socket = server.accept();
                    // 获取手机连接的地址及端口号
                    final String address = socket.getRemoteSocketAddress().toString();
                    System.out.println("连接成功，连接的设备为：" + address);

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                // 单线程锁
                                synchronized (this) {
                                    // 放进到Map中保存
                                    clientList.put(address, socket);
                                }
                                // 定义输入流
                                InputStream inputStream = socket.getInputStream();

                                byte[] buffer = new byte[1024];
                                int len;
                                while ((len = inputStream.read(buffer)) != -1) {
                                    String text = new String(buffer, 0, len);
                                    System.out.println("收到的数据为：\n" + text);
                                    // 在这里群发消息
                                    sendMsgAll();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                System.out.println("错误信息为：" + e.getMessage());
                            } finally {
                                synchronized (this) {
                                    System.out.println("关闭链接：" + address);
                                    clientList.remove(address);
                                }
                            }
                        }
                    }).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void Stop() {
            isExit = true;
            if (server != null) {
                try {
                    server.close();
                    System.out.println("已关闭server");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 群发的方法
    public static void sendMsgAll() {
        try {
            for (Socket socket : clientList.values()) {
                OutputStream outputStream = socket.getOutputStream();
                outputStream.write(("account=BluebankSW3&password=pass2222\n").getBytes("utf-8"));
                outputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}